/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.repository.collections;

import cz.mzk.oai.metadataRepository.metadata.MarcRecord;
import cz.mzk.oai.metadataRepository.metadata.MetadataFormatException;
import cz.mzk.oai.metadataRepository.metadata.RepeatableDataField;
import java.util.List;
import java.util.regex.Pattern;

/**
 *
 * @authorMartin Rehanek, Martin.Rehanek@gmail.com
 */
public class RecordAnalyzer {

    private Pattern mollMapPattern;//Starting with 'Moll-' except 'Moll-R' - (ai 'Moll-RKP-')
    private Pattern mollRkpPattern;//Starting with 'Moll-RKP'
    private Pattern oldMapPattern;//Starting with 'STMPa-'
    private Pattern skrinePattern;//Starting with 'Skř1' or 'Skř2' without 'Skř17'

    public enum BeforeAfterDecision {

        BEFORE, AFTER, ERROR
    }

    public RecordAnalyzer() {
        mollMapPattern = Pattern.compile("Moll-[^R,].*");
        mollRkpPattern = Pattern.compile("Moll-RKP.*");
        oldMapPattern = Pattern.compile("STMpa-.*");
        skrinePattern = Pattern.compile("Skř\\.[12][^7].*");
    }

    private boolean matches910b(Pattern pattern, MarcRecord record) {
        List<String> subfieldValues = record.getDatafield910().getSubfieldContents('b');
        for (String value : subfieldValues) {
            if (pattern.matcher(value).matches()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns true iff record contains datafield 910 with subfield b starting
     * with 'Moll-' except 'Moll-R' - (ai 'Moll-RKP-']
     */
    public boolean isMollMap(MarcRecord record) {
        return matches910b(mollMapPattern, record);
    }

    /**
     * Returns true iff record contains datafield 910 with subfield b starting
     * with 'Moll-RKP'
     */
    public boolean isMollRkp(MarcRecord record) {
        return matches910b(mollRkpPattern, record);
    }

    /**
     * Returns true iff record contains datafield 910 with subfield b starting
     * with 'STMPa-'
     */
    public boolean isOldMap(MarcRecord record) {
        return matches910b(oldMapPattern, record);
    }

    private boolean isInSkrine(MarcRecord record) {
        return matches910b(skrinePattern, record);
    }

    public boolean isPortrait(MarcRecord record) {
        //plati pro obsah 655a:
        //{fotografie}:1068 - ??? TODO
        //{fotografie,fotografie}:2 - ??? TODO
        //{fotografie,portréty}:181
        //{fotografie,portréty,grafické listy}:1
        //{grafické listy,portréty}713
        //{portréty}:232
        //{portréty,fotografie}:1
        //{staré tisky,portréty}:2
        RepeatableDataField datafield655 = record.getDatafield655();
        RepeatableDataField datafield650 = record.getDatafield650();
        return isInSkrine(record) && datafield655.hasDatafieldWithSubfieldValue('a', "portréty");
        //|| datafield650.subfieldsContain('a', "Schramova sbírka portrétů"));
    }

    public boolean isSchramVedutaOrMap(MarcRecord record) {
        //plati pro obsah 655a:
        //{fotografie,veduty}:11
        //{grafické listy,historické mapy}:1
        //{grafické listy,veduty}:244
        //{historické mapy}:4
        //{veduty,grafické listy}:110
        //anebo maji v 650 "Schramova sbírka vedut", "veduty", nebo "mosty"
        RepeatableDataField datafield655 = record.getDatafield655();
        RepeatableDataField datafield650 = record.getDatafield650();
        return isInSkrine(record) && (datafield655.hasDatafieldWithSubfieldValue('a', "veduty")
                || datafield655.hasDatafieldWithSubfieldValue('a', "historické mapy")
                || datafield650.hasDatafieldWithSubfieldValue('a', "Schramova sbírka vedut")
                || datafield650.hasDatafieldWithSubfieldValue('a', "veduty")
                || datafield650.hasDatafieldWithSubfieldValue('a', "mosty"));
    }

    public boolean isManuscript(MarcRecord record) {
        RepeatableDataField datafield072 = record.getDatafield072();
        RepeatableDataField datafield655 = record.getDatafield655();
        RepeatableDataField datafield533 = record.getDatafield533();
        return (datafield072.hasDatafieldWithSubfieldValue('x', "Rukopisy")
                && datafield655.hasDatafieldWithSubfieldValue('a', "Rukopisy")
                && !datafield655.hasDatafieldWithSubfieldValue('a', "Mikrodokumenty")
                && !datafield533.hasDatafieldWithSubfieldValue('a', "Mikrofilm"));
    }

    public BeforeAfterDecision decide1800(MarcRecord marcRecord) {
        try {
            if (marcRecord.isBefore1800()) {
                return BeforeAfterDecision.BEFORE;
            } else {
                return BeforeAfterDecision.AFTER;
            }
        } catch (MetadataFormatException e) {
            return BeforeAfterDecision.ERROR;
        }
    }
}
